    dojo.require('dojo.data.ItemFileReadStore');
    dojo.require('dojox.grid.DataGrid');
    dojo.require('dijit.form.Button');
    dojo.require('dijit.Dialog');
    dojo.require('dojo.parser');

//FIXME - change this to /index/fdpreregcases/group_ids so we can
//        fetch the (possibly) multiple group pool cases

    var preregCasesForGroupBaseUrl = '/index/fdgrouppreregcases/group_ids/';
    var preregCasesBaseUrl = '/index/fdpreregcases/funeral_dir_id/';
    var pendingCasesBaseUrl = '/index/fdpendingcases/funeral_dir_id/';
    var finishedCasesBaseUrl = '/index/fdfinishedcases/funeral_dir_id/';
	var finishedCaseReviewBaseUrl = '/index/finishedcase/case_number/';

	var fdPreregCasesUrl = '/index/fdmatchpreregcases';
	var enterVASBaseUrl = '/index/entervas/FDMS_case_num/';
	
	var preregGroupCasesURL = preregCasesForGroupBaseUrl + group_ids;
	var preregCasesURL = preregCasesBaseUrl + funeral_dir_id;
	var pendingCasesURL = pendingCasesBaseUrl + funeral_dir_id;
	var finishedCasesURL = finishedCasesBaseUrl + funeral_dir_id;
	
//    var preregCasesStore = new dojo.data.ItemFileReadStore({ url: preregCasesURL });
    var preregCasesStore = new dojo.data.ItemFileReadStore({ url: preregGroupCasesURL });
    var pendingCasesStore = new dojo.data.ItemFileReadStore({ url: pendingCasesURL });
    var finishedCasesStore = new dojo.data.ItemFileReadStore({ url: finishedCasesURL });

	function getSelectedPreregCaseNum()
	{
		
	}
	
	function getSelectedOpenCaseNum()
	{
		var items = pendingCasesGrid.selection.getSelected();
		if(items.length != 1) return -1;
	
		var selectedItem = items[0];
		var casenum = pendingCasesGrid.store.getValues(selectedItem, 'FDMS_case_num');
	
		return casenum;		
	}
	
	function getSelectedFinishedCaseNum()
	{
		var items = finishedCasesGrid.selection.getSelected();
		if(items.length != 1) return '';
	
		var selectedItem = items[0];
		var casenum = finishedCasesGrid.store.getValues(selectedItem, 'FDMS_case_num');
	
		return casenum;
	}
	
	function fdEditPreregBtnClick()
	{
		window.location = fdPreregCasesUrl;
	}
	
	function enterVASBtnClick()
	{
		var FDMScaseNum = getSelectedOpenCaseNum();
		if(FDMScaseNum == '')
		{
			alert('You must select an open case first.');
			return;
		}
		var enterVASurl = enterVASBaseUrl + FDMScaseNum;
		window.location = enterVASurl;
	}
	
	function fdreviewCaseBtnClick()
	{
		var FDMScaseNum = getSelectedFinishedCaseNum();
		if(FDMScaseNum == '')
		{
			alert('You must select a finished case first.');
			return;
		}
		var finishedCaseUrl = finishedCaseReviewBaseUrl + FDMScaseNum;
		window.location = finishedCaseUrl;
	}
	
    dojo.addOnLoad( function() {

	    preregCasesGrid = new dojox.grid.DataGrid({
		query: { id: '*' },
		store: preregCasesStore,
		structure: [
		    { field: "id", name: "ID", width:"30px" },
		    { field: "name", name: "Name", width:"auto" },
		    { field: "dob", name: "DOB", width:"auto" },
		    { field: "dod", name: "DOD", width:"auto" },
		    { field: "residence", name: "City, State", width:"auto" }
		],
		rowsPerPage: 5,
		selectionMode: 'single'
		}, 'preregCasesGrid1');

	    pendingCasesGrid = new dojox.grid.DataGrid({
		query: { FDMS_case_num: '*' },
		store: pendingCasesStore,
		structure: [
		    { field: "FDMS_case_num", name: "FDMS ID", width:"80px" },
		    { field: "name", name: "Name", width:"auto" },
		    { field: "dob", name: "DOB", width:"auto" },
		    { field: "dod", name: "DOD", width:"auto" },
		    { field: "informant", name: "Informant", width:"auto" }
		],
		rowsPerPage: 5,
		selectionMode: 'single'
		}, 'pendingCasesGrid1');

	    finishedCasesGrid = new dojox.grid.DataGrid({
		query: { FDMS_case_num: '*' },
		store: finishedCasesStore, 
		structure: [
		    { field: "FDMS_case_num", name: "FDMS ID", width:"80px" },
		    { field: "name", name: "Name", width:"auto" },
		    { field: "dob", name: "DOB", width:"auto" },
		    { field: "dod", name: "DOD", width:"auto" },
		    { field: "informant", name: "Informant", width:"auto" }
		],
		rowsPerPage: 5,
		selectionMode: 'single'
		}, 'finishedCasesGrid1');

		preregCasesGrid.startup();
		pendingCasesGrid.startup();
		finishedCasesGrid.startup();
	});
